/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileSelectPanel.java
 *
 * Created on 21.01.2010, 10:22:12
 */

package at.nhmwien.schema_mapping_tool;

import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.GridLayout;
import java.awt.Component;

/**
 *
 * @author wkoller
 */
public class FileSelectPanel extends DataSourceOptionsPanel {
    private JFileChooser fc;

    /** Creates new form FileSelectPanel */
    public FileSelectPanel() {
        initComponents();

        fc = new JFileChooser();
        fc.setAcceptAllFileFilterUsed(false);
    }

    public String getFileName() {
        return this.inputFileName.getText();
    }

    public void saveOptions() {
        Component childs[] = this.optionsPanel.getComponents();

        for( Component child : childs ) {
            if( !child.getName().startsWith( "label_" ) ) {
                this.processor.setOption( child.getName(), ((JTextField)child).getText());
            }
        }
    }

    public void loadOptions() {
        // Update available options
        this.optionsPanel.removeAll();
        GridLayout optionsLayout = new GridLayout(this.processor.getAvailableOptions().length, 2);
        this.optionsPanel.setLayout(optionsLayout);

        for( String option : this.processor.getAvailableOptions() ) {
            JTextField optionField = new JTextField();
            optionField.setName( option);
            optionField.setText(String.valueOf( this.processor.getOption(option) ));

            JLabel optionLabel = new JLabel(option + ":");
            optionLabel.setName( "label_" + option );

            this.optionsPanel.add(optionLabel);
            this.optionsPanel.add(optionField);
        }
        this.optionsPanel.validate();
    }

    public void setSourceName(String nSourceName) {
        super.setSourceName(nSourceName);

        String[] fpFileTypes = this.processor.getSupportedFileExtensions();
        fc.resetChoosableFileFilters();

        if (fpFileTypes != null) {
            String fpFTDescription = this.sourceName + " (*." + fpFileTypes[0];
            for (int j = 1; j < fpFileTypes.length; j++) {
                fpFTDescription += ", *." + fpFileTypes[j];
            }
            fpFTDescription += ")";

            FileNameExtensionFilter filter = new FileNameExtensionFilter( fpFTDescription, fpFileTypes );
            fc.addChoosableFileFilter(filter);
        }

        this.loadOptions();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputFileName = new javax.swing.JTextField();
        selectInputFileButton = new javax.swing.JButton();
        optionsPanel = new javax.swing.JPanel();

        inputFileName.setEditable(false);
        inputFileName.setText("File Name...");

        selectInputFileButton.setText("Select...");
        selectInputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectInputFileButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout optionsPanelLayout = new javax.swing.GroupLayout(optionsPanel);
        optionsPanel.setLayout(optionsPanelLayout);
        optionsPanelLayout.setHorizontalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 631, Short.MAX_VALUE)
        );
        optionsPanelLayout.setVerticalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(inputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(selectInputFileButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectInputFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectInputFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectInputFileButtonActionPerformed
        // Select an existing file
        String fcText = "Select Input File";
        if( !this.bInput ) fcText = "Select Output File";

        while( fc.showDialog(this, fcText ) == JFileChooser.APPROVE_OPTION ) {
            if( this.bInput && !fc.getSelectedFile().exists() ) {
                JOptionPane.showMessageDialog(this, "Please select an existing file!", "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                // Set the file name
                inputFileName.setText( fc.getSelectedFile().getAbsolutePath() );

                // Set the correct file type
                //inputFileFormatComboBox.setSelectedItem(((FileNameExtensionFilter) fc.getFileFilter()).getDescription().split(" ")[0]);

                // Get the extension of the file
                /*String fileName = fc.getSelectedFile().getName();
                String fileExt = fileName.substring(fileName.lastIndexOf(".") +1 ).toLowerCase();
                // Guess the file-format from the extension
                if( fileExt.equals("mrc") ) {
                    inputFileFormatComboBox.setSelectedIndex(0);
                }
                else if( fileExt.equals("xml") ) {
                    inputFileFormatComboBox.setSelectedIndex(1);
                }*/
                break;
            }
        }

}//GEN-LAST:event_selectInputFileButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField inputFileName;
    private javax.swing.JPanel optionsPanel;
    private javax.swing.JButton selectInputFileButton;
    // End of variables declaration//GEN-END:variables

}
